<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>标题</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui/css/layui.css">
    <link rel="stylesheet" href="/static/css/weadmin.css">
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>
<form class="layui-form searchForm">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">姓名:</label>
            <div class="layui-input-inline">
                <input type="text" name="realName" id="realName" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <div class="layui-input-inline">
                <button class="layui-btn" lay-submit lay-filter="formDemo">查询</button>
                <button class="layui-btn" lay-submit>清空</button>
            </div>
        </div>
    </div>
</form>
<table class="layui-hide" id="dataList" lay-filter="dataList"></table>

<script type="text/html" id="toolbarOrg">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add">添加账户</button>
        <button class="layui-btn layui-btn-sm" lay-event="del">批量删除</button>
    </div>
</script>

<script type="text/html" id="barDemo">
    {{# if(d.status==0 || d.status==1){ }}
    <a class="layui-btn layui-btn-xs layui-btn-disabled">启用</a>
    {{# }else if(d.status==2){ }}
    <a class="layui-btn layui-btn-xs" lay-event="disableBtn">禁用</a>
    {{# }else if(d.status==3){ }}
    <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="enabledBtn">启用</a>
    {{# } }}
    {{# if(d.orgType && d.orgType!='10'){ }}
	<a class="layui-btn layui-btn-xs" lay-event="assignProject">分配项目</a>
    {{# } }}
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>

<!--    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="changePass">重置密码</a>-->
</script>

<script type="text/javascript" src="/lib/layui/layui.js"></script>
<script src="/static/js/main.js"></script>
<script>
    var configureCom = {};
    layui.use(['jquery', 'configureCom', 'configure', 'sessionFun', 'table', 'matchDict'], function () {
        var $ = layui.jquery;
        var configure = layui.configure;
        var table = layui.table;
        var sessionFun = layui.sessionFun;
        var matchDict = layui.matchDict;
        configureCom = layui.configureCom;
        var cols = [[
            {type: 'checkbox'},
            {title: '序号', type: 'numbers'},
            {field: 'userName', title: '登录名'},
            {field: 'userCard', title: '身份证号码'},
            {field: 'realName', title: '真实姓名'},
            //{field: 'orgName', title: '所在单位'},
            {field: 'contact', title: '联系方式'},
            {
                field: '', title: '用户角色', templet: function (d) {
                    var html = '';
                    $(d.roleList).each(function (index, item) {
                        if (!index) {
                            html += item.roleName
                        } else {
                            html += ',' + item.roleName
                        }
                    });
                    return html;
                }
            },
            {
                field: '', title: '状态', templet: function (d) {
                    return matchDict.name(d.status, configure.ORG_STATUS);
                }
            },
            {title: '操作', toolbar: '#barDemo', width: 250}
        ]];
        configureCom.tableRender(configure.COM_USER_YEWU_LIST, {}, cols, 'dataList', 10, '', 'toolbarOrg');
        configureCom.tableSearch();
        //头工具栏事件
        table.on('toolbar(dataList)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            sessionFun.remove(configure.SESSION_LIST_DATA);
            switch (obj.event) {
                case 'add':
                    editItem('添加', configure.COM_USER_SAVE_LIST);
                    break;
                case 'del':
                    var data = checkStatus.data;
                    if (!data.length) {
                        top.layer.alert('请您至少选择一条数据后再提交！');
                        return false;
                    }
                    top.layer.confirm('您真的要删除选中的数据吗？', function (index, layero) {
                        top.layer.close(index);
                        var ids = '';
                        $(data).each(function (index, item) {
                            if (!index) {
                                ids = item.userId;
                            } else {
                                ids += ',' + item.userId;
                            }
                        });
                        deleteFun(ids);
                    });
                    break;
            }
        });
        //每行操作
        table.on('tool(dataList)', function (obj) {
            var data = obj.data;
            sessionFun.set(configure.SESSION_LIST_DATA, data);
            if (obj.event === 'del') {//查看详情
                deleteFun(data.userId);
            } else if (obj.event === 'showInfo') {
                showInfo(data.realName + ' - 信息查看');
                localStorage.getId = data.userId;
            } else if (obj.event === 'edit') {
                editItem('修改', configure.USER_UPDATE_HTTP)
            } else if (obj.event === 'enabledBtn') {
                configureCom.postDataAjax(configure.USER_UPDATE_STATUS, {'userId': data.userId, 'status': "1"}).then(function (res) {
                    if (res.code == 0) {
                        top.layer.msg('设置成功！');
                        configureCom.tableReload(true);
                    } else {
                        top.layer.alert('设置失败！')
                    }
                });
            }   else if (obj.event === 'disableBtn') {
                configureCom.postDataAjax(configure.USER_UPDATE_STATUS, {'userId': data.userId, 'status': "2"}).then(function (res) {
                    if (res.code == 0) {
                        top.layer.msg('设置成功！');
                        configureCom.tableReload(true);
                    } else {
                        top.layer.alert('设置失败！')
                    }
                });
            }  else if(obj.event === 'assignProject'){
				assignProjectDialog('分配项目', configure.RPJ_ASSIGN_WIN_HTML+'?userId='+data.userId)
			}
        });

        function deleteFun(ids) {
            debugger
            configureCom.postDataAjax(configure.USER_DELETE_HTTP, {userIds: ids}).then(function (res) {
                if (res.code == 0) {
                    top.layer.msg('删除成功！');
                    configureCom.tableReload(true);
                } else {
                    top.layer.alert('删除失败！');
                }
            });
        }

        //新增或修改字典详情
        function editItem(title, url) {
            top.layer.open({
                type: 2,
                title: title,
                area: configureCom.Psize1,
                content: configureCom.nocache(configure.COM_USER_DETAIL_HTML),
                btn: ['保存'],
                yes: function (index, layero) {
                    var iframeWin = top.window.frames['layui-layer-iframe' + index]; //得到iframe页的窗口对
                    var data = iframeWin.getData();
                    console.log(data);
                    configureCom.postDataAjax(url, data).then(function (res) {
                        if (res.code == 0) {
                            top.layer.close(index);
                            top.layer.msg('保存成功！');
                            configureCom.tableReload(true);
                        } else {
                            top.layer.alert('保存失败！');
                        }
                    });
                }
            });
        }
		
		/**
		 * 分配项目
		 * @param {Object} title
		 * @param {Object} url
		 */
		function assignProjectDialog(title, url) {
		    top.layer.open({
		        type: 2,
		        title: title,
		        area: configureCom.Psize1,
		        content: configureCom.nocache(url)
		    });
		}
		

    });

</script>
</body>
</html>